define(function(require) {
	var $ = require("jquery");
	var justep = require("$UI/system/lib/justep");
	var array = require("$UI/system/lib/base/array");
	var utils = require("$UI/system/components/justep/common/utils");
	var Model = function() {
		this.callParent();
	};
	
	Model.prototype.toUrl = function(url){
		return url ? require.toUrl(url) : "";
	};
	
	Model.prototype.bannerDataCustomRefresh = function(event) {
		var me = this.comp('contentsImg').getLength();
		var carousel = this.comp("carousel1");
		var i = 0;
		justep.Baas.sendRequest({
			"url" : "/nase/nase_action",
			"action" : "queryNase_banner",
			"async" : false,
			"params" : {},
			"success" : function(data) {

				array.each(data.rows, function(obj) {

					var fImgUrl = require.toUrl(obj.fImgUrl.value);
					var fUrl = require.toUrl(obj.fUrl.value);
					if (me > i) {
						$(carousel.domNode).find("img").eq(i).attr({
							"src" : fImgUrl,
							"pagename" : fUrl
						});
						if (i == 0) {
							localStorage.setItem("index_BannerImg_src", fImgUrl);
							localStorage.setItem("index_BannerImg_url", fUrl);
						}
					} else {
						carousel.add('<img src="' + fImgUrl + '" style="width:100%" height="160px" pagename="' + fUrl + '"/>');
					}
					i = i + 1;

				})

			}
		});

	};
	Model.prototype.productDataCustomRefresh = function(event) {
		var row1 = this.comp("rowl1");
		var row2 = this.comp("rowl2");
		var row3 = this.comp("rowl3");
		var h41 = this.getIDByXID('col3');
		var h51 = this.getIDByXID('col4');
		var h42 = this.getIDByXID('col5');
		var h52 = this.getIDByXID('col6');
		var h43 = this.getIDByXID('col7');
		var h53 = this.getIDByXID('col8');
		
		var i = 0;
		var heat = new Array();
		var hot = new Array();
		for(var x=0;x<6;x++){
			hot[x]=0;
		}
		justep.Baas.sendRequest({
			"url" : "/nase/nase_action",
			"action" : "queryNase_product",
			"async" : false,
			"params" : {},
			"success" : function(data) {
				array.each(data.rows, function(obj) {
					var fSales = obj.fSales.value;
					heat[i] = fSales;
					var fID = obj.fID.value;
					var fTitle = obj.fTitle.value;
					var fFtitle = obj.fFtitle.value;
					var fImgUrl = require.toUrl(obj.fImgUrl.value);
					var fNew = obj.fNew.value;

					if (1 > i && fNew == 1) {
						$(row1.domNode).find("div").attr({
							"alt" : fID
						});
						$(row1.domNode).find("img").eq(i).attr({
							"src" : fImgUrl,
							"alt" : fID
						});
						document.getElementById(h41).innerHTML = '<h4 class="newh4 cnumIbu" alt="' + fID + '">' + fTitle + '</h4>';
						document.getElementById(h51).innerHTML = '<h5 class="newh5 cnumIbu" alt="' + fID + '">' + fFtitle + '</h5>';
					}
					if (1 == i && fNew == 1) {
						$(row2.domNode).find("div").attr({
							"alt" : fID
						});
						$(row2.domNode).find("img").eq(0).attr({
							"src" : fImgUrl,
							"alt" : fID
						});
						document.getElementById(h42).innerHTML = '<h4 class="rightnewh4 cnumIbu" alt="' + fID + '">' + fTitle + '</h4>';
						document.getElementById(h52).innerHTML = '<h5 class="rightnewh5 cnumIbu" alt="' + fID + '">' + fFtitle + '</h5>';
					}
					if (1 < i && fNew == 1) {
						$(row3.domNode).find("div").attr({
							"alt" : fID
						});
						$(row3.domNode).find("img").eq(0).attr({
							"src" : fImgUrl,
							"alt" : fID
						});
						document.getElementById(h43).innerHTML = '<h4 class="newh4 cnumIbu" alt="' + fID + '">' + fTitle + '</h4>';
						document.getElementById(h53).innerHTML = '<h5 class="newh5 cnumIbu" alt="' + fID + '">' + fFtitle + '</h5>';

					}
					i = i + 1;
				})
			}
		});
		
		for(var x=0; x<hot.length;x++){
		var o="";
		
			for (var l = 0; l < heat.length; l++) {
				var z = heat[l];
				if (hot[x] < z) {
					hot[x] = z;
					o = l;
				}
			}
			if(o != "")
				heat[o]=0;
		}	
		
		var row4 = this.comp("rowl4");
		var cols1 = this.getIDByXID('cols1');
		var cols2 = this.getIDByXID('cols2');
		var colI1 = this.getIDByXID("colI1");
		var span1 = this.getIDByXID('cols3');
		var colI2 = this.getIDByXID("colI2");
		var span2 = this.getIDByXID('cols4');

		var row6 = this.comp("rowl6");
		var cols5 = this.getIDByXID('cols5');
		var cols6 = this.getIDByXID('cols6');
		var colI3 = this.getIDByXID("colI3");
		var span3 = this.getIDByXID('cols7');
		var colI4 = this.getIDByXID("colI4");
		var span4 = this.getIDByXID('cols8');
		var i = 0;
		var coll1 = this.comp('coll1');
		var coll2 = this.comp('coll2');
		var coll3 = this.comp('coll3');
		var coll4 = this.comp('coll4');
		justep.Baas.sendRequest({
			"url" : "/nase/nase_action",
			"action" : "queryNase_product",
			"async" : false,
			"params" : {},
			"success" : function(data) {
				array.each(data.rows, function(obj) {

					var fTitle = obj.fTitle.value;
					var fIcon = obj.fIcon.value;
					var fImgUrl = require.toUrl(obj.fImgUrl.value);
					var fSales = obj.fSales.value;
					var fID = obj.fID.value;
					if (i<6 && hot[0]==fSales) {
						$(row4.domNode).find("div").attr({
							"alt" : fID
						});
						$(row4.domNode).find("img").eq(0).attr({
							"src" : fImgUrl,
							"alt" : fID
						});
						if (fIcon != undefined)
							document.getElementById(cols1).innerHTML = '<span class="heatIcon cnumIbu" alt="' + fID + '">' + fIcon + '</span>';
							document.getElementById(cols2).innerHTML = '<span class="heatSpan cnumIbu" alt="' + fID + '">' + fTitle + '</span>';
							i = i + 1;
					}
					if (i<6 && hot[1]==fSales) {
						$(coll1.domNode).attr({
							"alt" : fID
						});
						document.getElementById(colI1).innerHTML = '<img src="' + fImgUrl + '" class="ssImg cnumIbu" alt="' + fID + '"/>'
						document.getElementById(span1).innerHTML = '<span class="ssSpan cnumIbu" alt="' + fID + '">' + fTitle + '</span>';
						i = i + 1;
					}
					if (i<6 && hot[2]==fSales) {
						$(coll2.domNode).attr({
							"alt" : fID
						});
						document.getElementById(colI2).innerHTML = '<img src="' + fImgUrl + '" class="ssImg cnumIbu" alt="' + fID + '"/>'
						document.getElementById(span2).innerHTML = '<span class="ssSpan cnumIbu" alt="' + fID + '">' + fTitle + '</span>';
						i = i + 1;
					}

					if (i<6 && hot[3]==fSales) {
						$(row6.domNode).find("div").attr({
							"alt" : fID
						});
						$(row6.domNode).find("img").eq(0).attr({
							"src" : fImgUrl,
							"alt" : fID
						});
						if (fIcon != undefined)
						document.getElementById(cols5).innerHTML = '<span class="heatIcon cnumIbu" alt="' + fID + '">' + fIcon + '</span>';
						document.getElementById(cols6).innerHTML = '<span class="heatSpan cnumIbu" alt="' + fID + '">' + fTitle + '</span>';
						i = i + 1;
					}
					if (i<6 && hot[4]==fSales) {
						$(coll3.domNode).attr({
							"alt" : fID
						});
						document.getElementById(colI3).innerHTML = '<img src="' + fImgUrl + '" class="ssImg cnumIbu" alt="' + fID + '"/>'
						document.getElementById(span3).innerHTML = '<span class="ssSpan cnumIbu" alt="' + fID + '">' + fTitle + '</span>';
						i = i + 1;
					}
					if (i<6 && hot[5]==fSales) {
						$(coll4.domNode).attr({
							"alt" : fID
						});
						document.getElementById(colI4).innerHTML = '<img src="' + fImgUrl + '" class="ssImg cnumIbu" alt="' + fID + '"/>'
						document.getElementById(span4).innerHTML = '<span class="ssSpan cnumIbu" alt="' + fID + '">' + fTitle + '</span>';
						i = i + 1;
					}
					
				})
			}
		});
	};


	Model.prototype.openPageClick = function(event) {
		this.comp('index').next();
		/*var pageName = event.currentTarget.getAttribute('pagename');
		if (pageName)
			justep.Shell.showPage(require.toUrl(pageName));*/
	};

	Model.prototype.details = function(event){
		var UserId=event.toElement.getAttribute("alt");
		localStorage.setItem('id', UserId);
		justep.Shell.showPage({url:"$UI/nase/detail.w"});
		
		
	};

	Model.prototype.indexActiveChange = function(event) {
		var to = event.to;
		if (to > 0) {
			this.comp('windowContainer' + to).load();
		}
		var button1 = this.comp("button1");
		var button2 = this.comp("button2");
		var button3 = this.comp("button3");
		var button4 = this.comp("button4");
		if (to === 0) {
			button1.set({
				"icon" : "img:$UI/nase/images/icon1l.png|"
			});
			button2.set({
				"icon" : "img:$UI/nase/images/icon2.png|"
			});
			button3.set({
				"icon" : "img:$UI/nase/images/icon3.png|"

			});
			button4.set({
				"icon" : "img:$UI/nase/images/icon4.png|"
			});
		}
		if (to === 1) {
			button2.set({
				"icon" : "img:$UI/nase/images/icon2l.png|"
			});
			button1.set({
				"icon" : "img:$UI/nase/images/icon1.png|"
			});
			button3.set({
				"icon" : "img:$UI/nase/images/icon3.png|"
			});
			button4.set({
				"icon" : "img:$UI/nase/images/icon4.png|"
			});
		}
		if (to === 2) {
			button3.set({
				"icon" : "img:$UI/nase/images/icon3l.png|"
			});
			button2.set({
				"icon" : "img:$UI/nase/images/icon2.png|"
			});
			button1.set({
				"icon" : "img:$UI/nase/images/icon1.png|"
			});
			button4.set({
				"icon" : "img:$UI/nase/images/icon4.png|"
			});
		}
		if (to === 3) {
			button4.set({
				"icon" : "img:$UI/nase/images/icon4l.png|"
			});
			button3.set({
				"icon" : "img:$UI/nase/images/icon3.png|"
			});
			button2.set({
				"icon" : "img:$UI/nase/images/icon2.png|"
			});
			button1.set({
				"icon" : "img:$UI/nase/images/icon1.png|"
			});
		}
	};

	Model.prototype.searchBtnClick = function(event){
		justep.Shell.showPage("search");
	};

	
	/*Model.prototype.c3Active = function(event){
		if(localStorage.getItem('LoginState') == " " || localStorage.getItem('LoginState') == undefined){
			justep.Shell.showPage("Login");
		}
	};
*/	
	Model.prototype.modelLoad = function(event) {
		justep.Shell.on("onHomeContent", this.onHomeContent, this);
	};
	Model.prototype.onHomeContent = function(event) {
		this.comp("index").to("c1");
	};
	
	Model.prototype.windowContainer2BeforeLoad = function(event){
		if(localStorage.getItem('LoginState') == " " || localStorage.getItem('LoginState') == undefined){
			justep.Shell.showPage("Login");
		}
	};

	

	

	return Model;
});